package net.xiangcaowuyu.dynamicdatasource.datasource;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class DynamicDataSourceContextHolder {

    private static final  ThreadLocal<String> contextHolder = new ThreadLocal<String>(){
        @Override
        protected String initialValue(){
            return "dynamicDataSourceOne";
        }
    };

    public static List<Object> dataSourceKeyList = new ArrayList<>();

    public static void setDataSourceKey(String key) throws Exception {
        if(!dataSourceKeyList.contains(key)){
            throw new Exception("数据库连接不存在");
        }
        contextHolder.set(key);
    }

    public static String getDataSourceKey(){
        return contextHolder.get();
    }

    public static void clearDataSourceKey(){
        contextHolder.remove();
    }

    /**
     * 添加数据库连接键
     * @param keys
     */
    public static void addDataSourceKeys(Collection<Object> keys){
        dataSourceKeyList.addAll(keys);
    }

}
